package com.example.skytakeoutserver.controller.admin

import com.example.serverbase.exception.BaseException
import com.example.skytakeoutserver.data.response.ApiResponse
import com.example.skytakeoutserver.data.vo.BusinessDataVO
import com.example.skytakeoutserver.data.vo.TurnoverReportVO
import com.example.skytakeoutserver.service.OrderService
import com.example.skytakeoutserver.service.UserService
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.tags.Tag
import lombok.RequiredArgsConstructor
import org.springframework.format.annotation.DateTimeFormat

import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestMapping
import org.springframework.web.bind.annotation.RestController
import reactor.core.publisher.Flux
import reactor.core.publisher.Mono
import java.time.LocalDate
import java.time.LocalDateTime

@RestController
@RequestMapping("/admin/report")
@RequiredArgsConstructor
@Tag(name = "报表管理", description = "报表管理")
class ReportController(
    val orderService: OrderService,
    val userService: UserService,
) {

    @Operation(summary = "营业额统计")
    @GetMapping("/turnover_statistics")
    fun turnoverStatistics(
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        begin: LocalDate,
        @DateTimeFormat(pattern = "yyyy-MM-dd")
        end: LocalDate,
    ): Mono<ApiResponse<TurnoverReportVO>> {
        TODO()
    }


    /**
     * 根据开始时间和结束时间获取日期列表
     */
    private fun getDataList(
        begin: LocalDate,
        end: LocalDate,
    ): Flux<LocalDate> {
        if (begin.isAfter(end)) {
            throw BaseException("开始日期不能大于结束日期")
        }

        return Flux.range(
            0,
            (end.toEpochDay() - begin.toEpochDay()).toInt() + 1
        ).map {
            begin.plusDays(it.toLong())
        }
    }

    /**
     * 获取营业数据
     */
    private fun getBusinessData(
        begin: LocalDateTime,
        end: LocalDateTime
    ):Mono<BusinessDataVO> {
//        val orderCountMono = orderService.coun
        TODO()
    }

    /**
     *
     */
    data class UserStatisticsData(
        val date: LocalDate,
        val total: Int,
        val newUser: Int
    )

    /**
     *
     */
    data class OrderStatisticsData(
        val date: LocalDate,
        val orderCount: Int,
        val validOrderCount: Int,
    )
}